﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace MatlabWork.Dal
{
    public class HttpService
    {
        private HttpClient _client = null;
        private string _baseUrl = "";
        private string _basetoken;


        public HttpService(string baseUrl, string basetoken)
        {

            _baseUrl = baseUrl;
            _basetoken = basetoken;
            _client = new HttpClient
            {
                BaseAddress = new Uri(_baseUrl)

            };
            _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        }


        public Task<string> POST(string apiroute, string parameterjson)
        {
            if (!string.IsNullOrEmpty(_basetoken))
            {
                _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(_basetoken);
                //  _client.DefaultRequestHeaders.Add("token", _basetoken);

            }
            //  string baseurl = System.Configuration.ConfigurationManager.AppSettings["apiurl"];
            StringContent content = new StringContent(parameterjson);
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var postresult = _client.PostAsync(apiroute, content);

            var msgbody = postresult.Result.Content.ReadAsStringAsync();
            var r = msgbody.Result;
            return msgbody;

        }

    }
}
